﻿using System.Web.Mvc;
using System.Web.Routing;
using SR = TinyForum.Web.Proxies.ServiceReference;
using System.Diagnostics;

namespace TinyForum.Web.Extensions.Mvc
{
    public abstract class ThemeController : Controller
    {
        protected override void Execute(RequestContext requestContext)
        {
            var themeName = "themeName";
            var defaultTheme = "Default";

            if (requestContext.HttpContext.Items[themeName] == null && requestContext.HttpContext.Request.Cookies.Get("theme") != null)
            {
                //first time load
                requestContext.HttpContext.Items[themeName] = requestContext.HttpContext.Request.Cookies.Get("theme").Value;
            }
            else
            {
                requestContext.HttpContext.Items[themeName] = defaultTheme;

                //var previewTheme = requestContext.RouteData.GetRequiredString("theme");
                var previewTheme = Site.Settings.SiteTheme ?? "Default";

                if (!string.IsNullOrEmpty(previewTheme))
                {
                    requestContext.HttpContext.Items[themeName] = previewTheme;
                }
            }

            base.Execute(requestContext);
        }
    }
}
